﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BUS;
using DAL;
namespace QuanLyNhaSach
{
    public partial class Form_PhieuNhap : DevComponents.DotNetBar.Office2007Form
    {
        Sach_BUS sach_bus;
        Sach_DTO sach_dto;
        NhaCungCap_BUS nhacungcap_bus;
        NhaCungCap_DTO nhacungcap_dto;
        PhieuNhap_BUS phieunhap_bus;
        PhieuNhap_DTO phieunhap_dto;
        ChiTietPhieuNhap_BUS chitietphieunhap_bus;
        ChiTietPhieuNhap_DTO chitietphieunhap_dto;
        public Form_PhieuNhap()
        {
            InitializeComponent();
        }
        
        private void Form_PhieuNhap_Load(object sender, EventArgs e)
        {
            nhacungcap_bus = new NhaCungCap_BUS();
            //load cac combobox

            cbxNhaCungCap.DataSource = nhacungcap_bus.LayDanhSachNCC();
            cbxNhaCungCap.DisplayMember = "Str_TenNCC";
            cbxNhaCungCap.ValueMember = "Str_MaNCC";

            txtNgayLap.Text = DateTime.Now.ToShortDateString();

            sach_bus = new Sach_BUS();
            cbxTenSach.DataSource = sach_bus.LayDanhSachSach();
            cbxTenSach.DisplayMember = "Str_TenSach";
            cbxTenSach.ValueMember = "Str_MaSach";

        }

        private void txtSoLuong_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {

            sach_bus = new Sach_BUS();
            sach_dto = new Sach_DTO();
            nhacungcap_bus = new NhaCungCap_BUS();
            nhacungcap_dto = new NhaCungCap_DTO();

            sach_dto = sach_bus.DanhSachSach_TheoMaSach(cbxTenSach.SelectedValue.ToString());
            nhacungcap_dto = nhacungcap_bus.DanhSachNCC_TheoMaNCC(cbxNhaCungCap.SelectedValue.ToString());

            //kiem tra nhap so lương
            if (int.Parse(txtSoLuong.Text) <= 0)
            {
                MessageBoxEx.Show("Số lượng nhập phải lớn hơn 0","Thông báo");
                txtSoLuong.Select();
                return;
            }
            //truong hop co roi lai them nua thi cap nhat lai dong đó.
            int flag = -1;//ko trung
            for (int i = 0; i < dtgPhieuNhap.Rows.Count; i++)
            {
                if (dtgPhieuNhap.Rows[i].Cells["Str_MaSach"].Value.ToString() == sach_dto.Str_MaSach)
                    flag = i;//trung thi cap nhat lai so luong
            }
            if (flag != -1)//da trung thi cap nhat lai so luong
            {
                int i_SoLuong = int.Parse(dtgPhieuNhap.Rows[flag].Cells["Str_SoLuong"].Value.ToString());
                dtgPhieuNhap.Rows[flag].Cells["Str_SoLuong"].Value = i_SoLuong + int.Parse(txtSoLuong.Text);
            }
            else
                dtgPhieuNhap.Rows.Add(sach_dto.Str_MaSach, sach_dto.Str_TenSach, nhacungcap_dto.Str_MaNCC, nhacungcap_dto.Str_TenNCC,int.Parse(txtSoLuong.Text));
        }
        int dong;
        private void dtgPhieuNhap_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            dong = e.RowIndex;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            dtgPhieuNhap.Rows.RemoveAt(dong);
        }

        private void btnHoaDonMoi_Click(object sender, EventArgs e)
        {
            dtgPhieuNhap.Rows.Clear();
        }

        private void btnInHoaDon_Click(object sender, EventArgs e)
        {
            phieunhap_bus = new PhieuNhap_BUS();            
            phieunhap_dto = new PhieuNhap_DTO();
            int flag = 1;
            //chen du lieu bang phieu nhap
            phieunhap_dto.Str_MaPhieuNhap = phieunhap_bus.MaPhieuNhapTang();
            phieunhap_dto.D_NgayLap = DateTime.Parse(txtNgayLap.Text);
            phieunhap_dto.Str_MaNV = "NV001";
            flag = phieunhap_bus.ThemPhieuNhap(phieunhap_dto);


            for (int i = 0; i < dtgPhieuNhap.Rows.Count; i++)
            {
                sach_bus = new Sach_BUS();
                sach_dto = new Sach_DTO();
                chitietphieunhap_bus = new ChiTietPhieuNhap_BUS();
                chitietphieunhap_dto = new ChiTietPhieuNhap_DTO();
                //cap nhat so luong ton cua sach                
                sach_dto.Str_MaSach = dtgPhieuNhap.Rows[i].Cells["Str_MaSach"].Value.ToString();
                sach_dto = sach_bus.DanhSachSach_TheoMaSach(sach_dto.Str_MaSach);
                int tem = sach_dto.I_SoLuong;
                int slsaunhap = tem + int.Parse(dtgPhieuNhap.Rows[i].Cells["Str_SoLuong"].Value.ToString());
                sach_dto.I_SoLuong = slsaunhap;
                flag = sach_bus.CapNhatSach(sach_dto);

                //chen du lieu banh chi tiet phieu nhap
                chitietphieunhap_dto.Str_MaPhieuNhap = phieunhap_dto.Str_MaPhieuNhap;
                chitietphieunhap_dto.Str_MaSach = sach_dto.Str_MaSach;
                chitietphieunhap_dto.I_SoLuong = int.Parse(dtgPhieuNhap.Rows[i].Cells["Str_SoLuong"].Value.ToString());
                chitietphieunhap_dto.Str_MaNCC = dtgPhieuNhap.Rows[i].Cells["Str_MaNCC"].Value.ToString();
                flag = chitietphieunhap_bus.ThemChiTietPhieuNhap(chitietphieunhap_dto);
            }
            if (flag == 1)
            {
                MessageBoxEx.Show("Lập phiếu nhập thành công","Thông báo");
            }
            else
                MessageBoxEx.Show("Lập phiếu nhập thất bại", "Thông báo");            
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtSoLuong_KeyPress_1(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true; 
        }

    }
}
